package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.util.CodeEmun;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    //    @Override
//    public ResultModel login(String username, String password, String code) {
////        1.验证验证码
//if(code.equals())
////        2.判断用户和密码
////        3得到结果返回
//        return null;
//    }
// 以后代码里面应该少些else 甚至不写，switch这个以后不要用，淘汰(设计模式，策略模式+工厂模式)
    @Override
    public ResultModel login(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        判断验证码是否正确
//        if (!userModel.getCode().equals(userModel.getSessionCode())) {
        if (false) {
//            resultModel.setCode(CodeEmun.ERROR_CODE.getCode());
//            return resultModel;
            return ResultModel.getInstance("error-code");
        }
        //            resultModel.setCode(200);
//            怎么判断用户名或者密码错误？
        UserDao userDao = new UserImpl();
//            数组和集合的区别？
        UserModel model = userDao.findByUsernameAndPassword(userModel);

//            boolean isHave = false;
//            if (model == null) {
//                isHave = false;
//            } else {
//                isHave = true;
//            }
//            isHave = model == null ? false : true;
        boolean isHave = model == null;
        if (isHave) {
//            resultModel.setCode(CodeEmun.ERROR.getCode());
//            return resultModel;
            return ResultModel.getInstance("error");
        }
//        resultModel.setCode(CodeEmun.SUCCESS.getCode());
//        return resultModel;
        return ResultModel.getInstance("success");
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
//        ResultModel resultModel=new ResultModel();
//        UserDao userDao = new UserImpl();
//        resultModel.setData(userDao.findAll(userModel));
//        return resultModel;
        return ResultModel.getInstance(userDao.findAll(userModel));
    }

    @Override
    public ResultModel add(UserModel userModel) {
//        ResultModel resultModel=new ResultModel();
//        UserDao userDao = new UserImpl();
//        resultModel.setCount(userDao.add(userModel));
//        return resultModel;
        return ResultModel.getInstance(userDao.add(userModel));
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
//        ResultModel resultModel=new ResultModel();
//        UserDao userDao = new UserImpl();
//        resultModel.setCount(userDao.getCount(userModel));
//        return resultModel;
        return ResultModel.getInstance(userDao.getCount(userModel));
    }

    @Override
    public ResultModel findById(UserModel userModel) {
//        ResultModel resultModel=new ResultModel();
//        UserDao userDao = new UserImpl();
//        resultModel.setData(userDao.findById(userModel));
        return ResultModel.getInstance(userDao.findById(userModel));
    }

    @Override
    public ResultModel update(UserModel userModel) {
//        ResultModel resultModel=new ResultModel();
//        UserDao userDao = new UserImpl();
//        resultModel.setCount(userDao.update(userModel));
//        return resultModel;
        return ResultModel.getInstance(userDao.update(userModel));
    }

    @Override
    public ResultModel del(UserModel userModel) {
        return ResultModel.getInstance(userDao.del(userModel));
    }

    @Override
    public ResultModel enable(UserModel userModel) {
        return ResultModel.getInstance(userDao.enable(userModel));
    }
}
